-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tanya – Pine #25
base: master
Are you sure you want to change the base?
Tanya – Pine #25
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨💫 Nice work! I left a few comments below.
For the comprehension questions,
How is a heap different from a BST? --> Your answer is correct, but what is the max/min heap property?
Were the heap_up
& heap_down
methods useful? Why? --> You do use recursion in heap_up
and heap_down
but why is recursion useful here?
Let me know what questions you have.
🟢
|
||
def heap_sort(list): | ||
""" This method uses a heap to sort an array. | ||
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: O(log n) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👀 Time complexity is going to be O(nlogn) as you iterate through n items in list
twice, and within each loop perform add
or remove
which are both O(log n) operations
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: O(log n) | ||
Space Complexity: O(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨ Nice however space complexity is going to be O(log n) here because of the recursive call stack of heap_up
Time Complexity: ? | ||
Space Complexity: ? | ||
Time Complexity: O(log n) | ||
Space Complexity: O(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨ However space complexity here will be O(log n) because of the recursive call stack of heap_down
Time complexity: ? | ||
Space complexity: ? | ||
Time complexity: O(1) | ||
Space complexity: O(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨
Time complexity: ? | ||
Space complexity: ? | ||
Time complexity: O(log n) | ||
Space complexity: O(log n) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨
if len(self.store) > (left_child) and self.store[index].key > self.store[left_child].key: | ||
self.swap(index, (left_child)) | ||
self.heap_down((left_child)) | ||
self.heap_down(index) | ||
elif len(self.store) > (right_child) and self.store[index].key > self.store[right_child].key: | ||
self.swap(index, (right_child)) | ||
self.heap_down((right_child)) | ||
self.heap_down(index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can reduce your recursive calls to heap_down
here. Instead of choosing to swap with the left child automatically if it exists, choose to swap index
with whichever child is smaller.
If you swap with the smaller of the two children, that child (as the new parent of index
and the other child) will automatically be smaller than both of it's new children and you won't have to call heap_down
on it again.
|
||
if self.store[parent_index].key > self.store[index].key: | ||
self.swap(parent_index, index) | ||
self.heap_up(parent_index) | ||
|
||
def heap_down(self, index): | ||
""" This helper method takes an index and | ||
moves the corresponding element down the heap if it's | ||
larger than either of its children and continues until | ||
the heap property is reestablished. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works, but review my comment below to see how you might improve upon this solution ⬇️
Heaps Practice
Congratulations! You're submitting your assignment!
Comprehension Questions
heap_up
&heap_down
methods useful? Why?